package com.itheima.service.Impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public List<User> list() {
       List<User> list= userDao.findAll();
        for (User user : list) {
            //获取id
            Long id = user.getId();
            //查询对应得role集合对象
            List<Role> byUserId = userDao.findRoleByUserId(id);
            user.setRoleList(byUserId);
        }
        return list;
    }

    @Override
    public void save(User user, long[] roleIds) {
        //先向 sys_user表中储存数据
       Long userId= userDao.save(user);
       //再向 sys_user_role关系表中存多条数据
        userDao.saveUserRoleRel(userId,roleIds);
    }

    @Override
    public void del(Long userId) {
        //1、删除sys_user_role关系表
        userDao.delUserRoleRel(userId);
        //2、删除sys_user表
        userDao.del(userId);
    }
}
